package com.ssy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.mapper.DictionariesMapper;
import com.ssy.system.pojo.Dictionaries;
import com.ssy.system.service.IDictionariesService;
import com.ssy.system.vo.DictionariesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author ssy
 */
@Service
public class DictionariesServiceImpl extends ServiceImpl<DictionariesMapper, Dictionaries> implements IDictionariesService {

    @Autowired
    private DictionariesMapper dictionariesMapper;

    @Override
    public PageResult loadAllDictionaries(DictionariesVo dic) {
        Page<Dictionaries> page = new Page<>(dic.getPage(), dic.getLimit());
        IPage<Dictionaries> selectPage = this.dictionariesMapper.selectPage(page, new LambdaQueryWrapper<Dictionaries>()
                .like(StringUtils.hasText(dic.getName()), Dictionaries::getName, dic.getName())
                .like(StringUtils.hasText(dic.getType()), Dictionaries::getType, dic.getType())
                .eq(dic.getState() != null, Dictionaries::getState, dic.getState())
                .ge(dic.getBeginTime() != null, Dictionaries::getCreateTime, dic.getBeginTime())
                .le(dic.getEndTime() != null, Dictionaries::getCreateTime, dic.getEndTime())
        );
        return new PageResult(selectPage.getTotal(), selectPage.getRecords());
    }

    @Override
    public Result saveDictionaries(Dictionaries dic) {
        if (dic == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        int i = this.dictionariesMapper.insert(dic);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateDictionaries(Dictionaries dic) {
        if (dic == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.dictionariesMapper.updateById(dic);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result bathDictionaries(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        int i = this.dictionariesMapper.deleteBatchIds(idList);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result deleteDictionaries(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.dictionariesMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }
}
